package com.longrise.android;

import java.io.File;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import android.app.Application;
import android.util.Log;

import com.longrise.LEAP.BO.Extend.leapcodevalue;
import com.longrise.LEAP.Base.Objects.UserInfo;
import com.longrise.LEAP.Base.Service.LEAPServiceClient;
import com.longrise.LWFP.BLL.Cache.OrganTree.OrganizationTree;

public class Global extends Application
{
	//全局事件说明LSMsgCall(int type, Object... parameters){
	/*
	 * 以下为已经被占用数值，各业务模块不可再使用，建议各业务模块使用正整数作为事件值
	 	type == GlobalEventCode.FormClose : 窗口关闭事件 
	 	type == GlobalEventCode.Message ：短信事件
	 	type == GlobalEventCode.Camera: 相机事件
	 	type == GlobalEventCode.Eben_Pdf:	ERenEBen pdf
	 	type == GlobalEventCode.ChooserFile:	文件选择器
	 */
	//全局事件说明}
	
	// 通用属性定义,不可变更lilw{
	private static Global _instance = null;
	private boolean _isdebugger = true;
	private LEAPServiceClient _client = null;
	private String userflag = null;
	private String pwd = null;//暂时使用（用于消息功能登录）
	private String serverUrl = null;// "http://10.1.37.248:8080/BAOA/LEAP/Service/RPC/RPC.DO";
	private String updateUrl = null;
	private String versionName = "V1.0";
	private int versionCode = -1;
	private String leapAppName = "BAOA"; //leap平台中，对当前App管理的名字，用于版本更新模块
	private String appCode = null;// 用于leap登录服务
	private String appId = null;// 用于leap登录服务
	private String appValue = null;// 用于leap登录服务
	private String appArea = null;
	private String systemName = null;
	private int fontType = 0;//字体类型 0：android默认，1：楷体，2：宋体
	private String welcomeString = null;//欢迎语
	private String sharedPreferencesName = "baoaInfo";//本地用户操作习惯数据缓存名
	private OrganizationTree _orgTree = null;//机构树，用于流程发送
	private UserInfo userinfo = null;
	private int msgTask = 0;//消息提醒间隔（访问服务时间间隔）
	private Hashtable<String,leapcodevalue[]> _codeValue = null; //代码表
	private String _serviceName = "leap";
	private boolean isHaveNet = true;
	// 通用属性定义}

	@Override
	public void onCreate() {
		super.onCreate();
		Global.getInstance();
	}
	// 业务属性定义，将根据业务需求变更{

	// 业务属性定义，将根据业务需求变更}

	public void setServiceName(String name)
	{
		this._serviceName = name;
	}
	public String getServiceName()
	{
		return this._serviceName;
	}
	
	public boolean isDebugger()
	{
		return this._isdebugger;
	}
	public void setDebugger(boolean b)
	{
		this._isdebugger = b;
	}
	
	
	
	public boolean isHaveNet() {
		return isHaveNet;
	}

	public void setHaveNet(boolean isHaveNet) {
		this.isHaveNet = isHaveNet;
	}
	
	public Hashtable<String,leapcodevalue[]> getCodeValue()
	{
		if(null==this._codeValue)
		{
			this._codeValue = new Hashtable<String, leapcodevalue[]>();
		}
		return this._codeValue;
	}
	public void setMsgTask(int i)
	{
		this.msgTask = i;
	}
	public int getMsgTask()
	{
		return this.msgTask;
	}
	public LEAPServiceClient getClient()
	{
		if (null == this._client)
		{
			FrameworkManager.getInstance().setServerUrl(this.serverUrl);
			FrameworkManager.getInstance().setServiceName(this._serviceName);
			this._client = FrameworkManager.getInstance().getClient();					
		}
		return this._client;
	}	
	
	public void setPwd(String p)
	{
		this.pwd = p;
	}
	public String getPwd()
	{
		return this.pwd;
	}
	public void setUserInfo(UserInfo uinfo)
	{
		this.userinfo = uinfo;
	}
	public UserInfo getUserInfo()
	{
		return this.userinfo;
	}
	public void setOrgTree(OrganizationTree tree)
	{
		this._orgTree = tree;
	}
	public OrganizationTree getOrgTree()
	{
		return this._orgTree;
	}
	public void setSPName(String spname)
	{
		this.sharedPreferencesName = spname;
	}
	public String getSPName()
	{
		return this.sharedPreferencesName;
	}
	/**
	 * 标题欢迎语
	 * @param
	 */
	public void setWelcomeString(String s)
	{
		this.welcomeString = s;
	}
	/**
	 * 标题欢迎语
	 * @param
	 */
	public String getWelcomeString()
	{
		return this.welcomeString;
	}
	
	/**
	 * leap平台中，对当前App管理的名字，用于版本更新模块
	 * @param name
	 */
	public void setLeapAppName(String name)
	{
		this.leapAppName = name;
	}
	/**
	 * leap平台中，对当前App管理的名字，用于版本更新模块
	 * @param
	 */
	public String getLeapAppName()
	{
		return this.leapAppName;
	}
	/**
	 * 字体类型（0：android默认，1：楷体，2：宋体）
	 * @return
	 */
	public int getFontType()
	{
		return this.fontType;
	}
	/**
	 * 字体类型（0：android默认，1：楷体，2：宋体）
	 * @return
	 */
	public void setFontType(int t)
	{
		this.fontType = t;
	}
	/**
	 * 当前版本号
	 * 
	 * @return
	 */
	public int getVersionCode()
	{
		return versionCode;
	}

	/**
	 * 当前版本号
	 * 
	 * @return
	 */
	public void setVersionCode(int versionCode)
	{
		this.versionCode = versionCode;
	}

	/**
	 * 当前版本名
	 * 
	 * @return
	 */
	public String getVersionName()
	{
		return versionName;
	}

	/**
	 * 当前版本名
	 * 
	 * @return
	 */
	public void setVersionName(String vname)
	{
		this.versionName = vname;
	}

	public String getAppCode()
	{
		return appCode;
	}

	public void setAppCode(String appCode)
	{
		this.appCode = appCode;
	}

	public String getAppId()
	{
		return appId;
	}

	public void setAppId(String appId)
	{
		this.appId = appId;
	}

	public String getAppValue()
	{
		return appValue;
	}

	public void setAppValue(String appValue)
	{
		this.appValue = appValue;
	}
	public String getAppArea()
	{
		return appArea;
	}

	public void setAppArea(String appArea)
	{
		this.appArea = appArea;
	}
	public String getSystemName()
	{
		return systemName;
	}

	public void setSystemName(String systemName)
	{
		this.systemName = systemName;
	}
	/**
	 * 升级路径
	 * 
	 * @return
	 */
	public String getUpdateUrl()
	{
		return updateUrl;
	}

	/**
	 * 升级路径
	 * 
	 * @return
	 */
	public void setUpdateUrl(String url)
	{
		this.updateUrl = url;
	}

	/**
	 * leap服务路径
	 * 
	 * @return
	 */
	public String getServerUrl()
	{
		return serverUrl;
	}

	/**
	 * leap服务路径
	 * 
	 * @return
	 */
	public void setServerUrl(String url)
	{
		if (null != url && !"".equals(url))
		{
			if (url.lastIndexOf("/") != url.length() - 1)
			{
				url += "/";
			}
		}

		this.serverUrl = url;
	}

	/**
	 * 获取当前登录用户
	 * 
	 * @return
	 */
	public String getUserflag()
	{
		return userflag;
	}

	/**
	 * 设置当前登录用户，只能在登录模块设置
	 * 
	 * @param userflag
	 */
	public void setUserflag(String userflag)
	{
		this.userflag = userflag;
	}

	/**
	 * 允许在UI线程中调用服务 此功能不开放，所有服务调用必须在后台线程中执行 by lilw
	 */
	/*
	 * public void setWebServer() { try {
	 * if(android.os.Build.VERSION.SDK_INT>10) { StrictMode.setThreadPolicy(new
	 * StrictMode
	 * .ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork
	 * ().penaltyLog().build()); StrictMode.setVmPolicy(new
	 * StrictMode.VmPolicy.Builder
	 * ().detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath().build()); } }
	 * catch (Exception e) { } }
	 */

	public Global()
	{

	}

	public static Global getInstance()
	{
		if (null == _instance)
		{
			synchronized (Global.class) 
			{
				if (null == _instance)
				{
					_instance = new Global();
				}
			}
		}
		
		return _instance;
	}

	/**
	 * 调用leap平台服务
	 * 
	 * @param service
	 * @param classType
	 * @param parameters
	 * @return
	 */
	public Object call(String service, Class<?> classType, Object... parameters)
	{
		try
		{
			if (null == _client)
			{
				_client = getClient();
			}
			if(null!=_client)
			{
				return _client.call(service, classType, parameters);
			}
		}
		catch (Exception ex)
		{
			
		}
		return null;
	}

	public void clean()
	{
		this._client = null;
		FrameworkManager.getInstance().restClient();		
	}
	

	
	public void onDistory()
	{		
		_codeValue = null;
		_client = null;
		userflag = null;
		serverUrl = null;
		updateUrl = null;
		versionName = null;
		appCode = null;
		appId = null;
		appValue = null;
		_instance = null;
		userinfo = null;
	}
}
